import { createRouter, createWebHashHistory } from 'vue-router'

import Login from '../Login.vue'
import Home from '../Home.vue'
import Error from '../Error.vue'


const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            name: 'Login',
            component: Login
        },
        {
            path: '/home',
            name: 'Home',
            component: Home
        },
        {
            path: '/error',
            name: 'Error',
            component: Error
        },
    ]
});

router.beforeEach((to, from) => {
    console.log('beforeEach to=>', to);
    console.log('beforeEach from=>', from);
    let msg = JSON.parse(sessionStorage.getItem('userInfo'));
    console.log(msg);
    // console.log('----------msg----------',msg.acc);
    // console.log('----------msg----------',msg.pwd);
    if (to.name == 'Home' | to.path == '/home') {
        if (msg) {
            if (msg.acc == 'admin' && msg.pwd == 'admin') return true
            else return { path: '/error' }
        } else {
            return { path: '/' }
        }
    } else if (to.path == '/error') {
        if (msg) {
            if (msg.acc == 'admin' && msg.pwd == 'admin') {
                return { path: '/home' }
            } else {
                return true
            }
        } else return { path: '/' }
    } else if (to.path == '/') {
        if (msg) {
            if (msg.acc == 'admin' && msg.pwd == 'admin') {
                return { path: '/home' }
            } else {
                return true
            }
        } else return true
    } else return true
})

export default router